package io.libp2p.example.chat;

import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.protocols.Protocol;

import java.net.URI;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Collections;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SpdSocketClient extends WebSocketClient {
    public static String TAG = "SpdSocketClient";

    public interface OnReceiveMessage{
        void onMessage(String message);
    }

    public interface OnConnectOpen{
        void OnConnectOpen();
        void OnConnectClosed();
    }

    OnReceiveMessage onReceiveMessage = null;

    OnConnectOpen onConnectOpen = null;

    Handler mainHandler = new Handler(Looper.getMainLooper());

    public SpdSocketClient(URI serverUri) {
        super(serverUri);

//        SSLSocketFactory factory = null;
//        try {
//            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
//            sslContext.init(null, new TrustManager[]{new X509TrustManager() {
//                @Override
//                public void checkClientTrusted(X509Certificate[] chain, String authType) {}
//                @Override
//                public void checkServerTrusted(X509Certificate[] chain, String authType) {}
//                @Override
//                public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
//            }}, new SecureRandom());
//            factory = sslContext.getSocketFactory();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        this.setSocketFactory(factory);
    }

    @Override
    public void onOpen(ServerHandshake handshake) {
        MyLog.D(TAG,"SpdSocketClient onOpen");

        if(onConnectOpen != null){
            onConnectOpen.OnConnectOpen();
        }
    }

    @Override
    public void onMessage(String message) {
        MyLog.D(TAG,"SpdSocketClient onMessage:"+message);

        if(onReceiveMessage != null){
            mainHandler.post(()-> onReceiveMessage.onMessage(message));
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        MyLog.D(TAG,"onClose reason:"+reason);
        if(onConnectOpen != null){
            onConnectOpen.OnConnectClosed();
        }
    }

    @Override
    public void onError(Exception ex) {
        MyLog.D(TAG,"onError ex:"+ex);
    }

    public void setOnReceiveMessage(OnReceiveMessage onReceiveMessage) {
        this.onReceiveMessage = onReceiveMessage;
    }

    public void setOnConnectOpen(OnConnectOpen onConnectOpen) {
        this.onConnectOpen = onConnectOpen;
    }

    public void sendSpdJson(SpdJson spdJson){
        mainHandler.post(()->{
            String json = new Gson().toJson(spdJson);
            send(json);
            MyLog.D(TAG, "sendSpdJson:"+json);
        });
    }
}
